var DBFactory = require('../db/mysql_db');
var async = require('async');
var collectionDao = module.exports;

function isExist(data, connection, callback) {
    var sql = 'select count(*) as count, id from tao_acc_collection where account = ? ';
    connection.query(sql, [data.account], function(err, result) {
        if(err) {
            callback(err);
        } else {
            var info = {};
            info.isExist = (result && result[0] && result[0].count > 0 ) ? true : false;
            info.id = (result && result[0] && result[0].id ) ? result[0].id : undefined;
            callback(null, info);
        }
    });
}


function getRecommenderAndProvinceAgent(data, connection, callback) {
    var sql = 'select * from tao_acc_collection where id = ? ';
    connection.query(sql, [data.id], function(err, result) {
        if(err) {
            callback(err);
        } else {
            if(result.length > 0) {
                callback(null, result);
            } else {
                callback('地址不合法，请重试！');
            }
        }
    });
}
function getRecommender2(info, connection, callback) {
    var sql = 'select recommender from tao_acc_collection where account = ? ';
    if(info.recommender) {
        connection.query(sql, [info.recommender], function(err, result) {
            if(err) {
                callback(err);
            } else {
                info.recommender2 = (result && result[0] && result[0].recommender) ? result[0].recommender : undefined;
                callback(null, info);
            }
        });
    } else {
        callback(null, info);
    }
}
function registerToDb(info, connection, callback) {
    var sql = 'insert into tao_acc_collection set ? ';
    connection.query(sql, info, function(err, result) {
        if(err) {
            callback('登记时发生错误，请重试！');
        } else {
            callback(null, {id: result.insertId});
        }
    });
}

collectionDao.register = function(data, cb) {
    if(!data) return cb(new Error(500));
    DBFactory.getConnection(function (error, connection) {
        if (error) {
            cb(error);
        } else {
            async.waterfall([
                function(callback) {
                    isExist(data, connection, callback);
                },
                function(result, callback) {
                    data.isExist = result.isExist
                    if(result.isExist) {
                        callback(null, {id: result.id});
                    } else {
                        getRecommenderAndProvinceAgent(data, connection, callback);
                    }
                },
                function(result, callback) {
                    if(!data.isExist) {
                        var info = {};
                        info.account = data.account;
                        info.agent_2 = (result && result[0] && result[0].agent_2) ? result[0].agent_2 : undefined;
                        info.recommender = (result && result[0] && result[0].account) ? result[0].account : undefined;
                        getRecommender2(info, connection, callback);
                    } else {
                        callback(null, result);
                    }
                },
                function(info, callback) {
                    if(!data.isExist) {
                        registerToDb(info, connection, callback);
                    } else {
                        callback(null, info);
                    }
                }
            ],function(err, result) {
                if(err) {
                    connection.release();
                    cb(err);
                } else {
                    connection.release();
                    cb(null, result);
                }
            });
        }
    });
}

collectionDao.getSubList = function(data, cb) {
    if(!data) return cb(new Error(500));
    var sql_total = 'select count(*) as count, recommender from tao_acc_collection where recommender = ? and account != ? ';
    var sql_data = 'select * from tao_acc_collection where recommender = ?  and account != ? ';
    DBFactory.poolQuery(sql_total, [data.account, data.account],
        function () {
            cb('no collection');
        },
        function (err, total) {
            if (err) {
                console.log(err);
                cb(err);
                return;
            }
            DBFactory.poolQuery(sql_data, [data.account, data.account],
                function () {
                    cb('no collection');
                },
                function (err, result) {
                    if (err) {
                        console.log(err);
                        cb(err);
                        return;
                    } else {
                        var list = {};
                        list.total = total[0].count;
                        list.data = result;
                        cb(null, list);
                    }
                }
            );
        }
    );
}

